package com.yami.shop.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageParam;

import com.yami.shop.dao.UserMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.yami.shop.service.UnlockRecordService;
import com.yami.shop.bean.model.UnlockRecord;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 * 解锁记录 前端控制器
 * </p>
 *
 * @author chajiu
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/unlock-record")
public class UnlockRecordController {


    @Autowired
    private UnlockRecordService UnlockRecordService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页获取
    */
     @GetMapping("/page")
     @PreAuthorize("@pms.hasPermission('admin:UnlockRecord:page')")
     public ResponseEntity<IPage<UnlockRecord>> page(UnlockRecord entity,PageParam<UnlockRecord> page) {
         IPage<UnlockRecord> iPage = UnlockRecordService.page(page, new LambdaQueryWrapper<UnlockRecord>());
         for(UnlockRecord ipage : iPage.getRecords()){
             User user = userMapper.selectById(ipage.getFromUserId());
             User user1 = userMapper.selectById(ipage.getUnlockedUserId());
             ipage.setName(user.getNickName());
             ipage.setUnlockedUserName(user1.getNickName());
             ipage.setUnlockedUserPic(user1.getPic());
         }
         return ResponseEntity.ok(iPage);
     }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('admin:UnlockRecord:save')")
    public ResponseEntity<Void> save(@RequestBody UnlockRecord entity) {
        UnlockRecordService.save(entity);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('admin:UnlockRecord:info')")
    public ResponseEntity<UnlockRecord> info(@PathVariable("id") String id){
        UnlockRecord entity = UnlockRecordService.getById(id);
            return ResponseEntity.ok(entity);
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:UnlockRecord:update')")
    public ResponseEntity<Void> update(@RequestBody UnlockRecord entity){
        UnlockRecordService.updateById(entity);
            return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin:UnlockRecord:delete')")
    public ResponseEntity<Void> delete(@RequestBody String[]ids){
        UnlockRecordService.removeByIds(Arrays.asList(ids));
            return ResponseEntity.ok().build();
    }


}

